package cn.yr.controller.manage;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.yr.common.oss.OssKit;
import cn.yr.common.util.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@PropertySource({"classpath:oss.properties"})
public class FileController {

	@Value("${upy.baseDisc}")
	private String baseDisc;

	@RequestMapping("/upload")
	public AjaxResult upload(MultipartFile file){
		try {
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			Snowflake snowflake = IdUtil.createSnowflake(1, 1);
			long id = snowflake.nextId();
			String imgName = baseDisc+"/"+id+suffix;
			String upload = OssKit.upload(file.getBytes(), imgName);
			return AjaxResult.success(upload);
		}catch (Exception e){
			return AjaxResult.error(e.getMessage());
		}
	}
}
